package edu.midir.demo.controller;

import edu.midir.demo.pojo.ResultBean;
import edu.midir.demo.pojo.domain.UserEntity;
import edu.midir.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
//@CrossOrigin(allowCredentials = "true")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping
    public ResultBean<UserEntity> getAUser(){

        UserEntity yangzhi = userService.findUserByNickName("yangzhi");
        log.info("------------------"+yangzhi.toString());
        return ResultBean.success(yangzhi);
    }

//    @GetMapping
//    public String getAUser(){
//
//        return "null";
//    }
    @PostMapping
    public void saveUser(@RequestBody UserEntity userEntity){
         userService.saveUser(userEntity);
    }

    @GetMapping("/cookie")
    public ResultBean<String> getCookie(@CookieValue(value="cookie1")String cookie1){
        log.info("test get cookie");
        return ResultBean.ok("getCookie"+cookie1);
    }

    @GetMapping("/header")
    public ResultBean<String> getCookie(@RequestHeader("x-header1")String header1,
                                        @RequestHeader("x-header2")String header2
                                        ){
        String s = header1 + " " + header2;
        log.info(s);
        return ResultBean.ok("getHeader"+" "+s);
    }


}